using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using Controls;

namespace PCR.PCRControls
{
	public partial class NumSpan : UserControl, INotifyPropertyChanged, IComponentConnector
	{
		public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(NumSpan));

		public static readonly DependencyProperty StepProperty = DependencyProperty.Register("Step", typeof(double), typeof(NumSpan));

		public static readonly DependencyProperty FloatNumProperty = DependencyProperty.Register("FloatNum", typeof(float), typeof(NumSpan));

		public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(double), typeof(NumSpan), new PropertyMetadata(double.MaxValue));

		public static readonly DependencyProperty MinValueProperty = DependencyProperty.Register("MinValue", typeof(double), typeof(NumSpan), new PropertyMetadata(double.MinValue));

		public double Value
		{
			get
			{
				return (double)GetValue(ValueProperty);
			}
			set
			{
				if (value > MaxValue)
				{
					value = MaxValue;
				}
				if (value < MinValue)
				{
					value = MinValue;
				}
				value = (float)Convert.ToInt32(value * (double)FloatNum) / FloatNum;
				SetValue(ValueProperty, value);
				cValue.Text = value.ToString();
			}
		}

		public double Step
		{
			get
			{
				return (double)GetValue(StepProperty);
			}
			set
			{
				SetValue(StepProperty, Step);
			}
		}

		public float FloatNum
		{
			get
			{
				return (float)GetValue(FloatNumProperty);
			}
			set
			{
				SetValue(FloatNumProperty, FloatNum);
			}
		}

		public double MaxValue
		{
			get
			{
				return (double)GetValue(MaxValueProperty);
			}
			set
			{
				SetValue(MaxValueProperty, MaxValue);
			}
		}

		public double MinValue
		{
			get
			{
				return (double)GetValue(MinValueProperty);
			}
			set
			{
				SetValue(MinValueProperty, MinValue);
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;

		public NumSpan()
		{
			InitializeComponent();
			TextBoxEx textBoxEx = cValue;
			RoutedEventHandler value = delegate
			{
				cValue.Text = ((float)Convert.ToInt32(Value * (double)FloatNum) / FloatNum).ToString();
				cValue.Focus();
			};
			textBoxEx.Loaded += value;
		}

		public void OnPropertyChanged(string propName)
		{
			if (string.IsNullOrEmpty(propName))
			{
				throw new ArgumentException("propName");
			}
			if (this.PropertyChanged != null)
			{
				this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
			}
		}

		private void Up_Click(object sender, RoutedEventArgs e)
		{
			Value += Step;
		}

		private void Down_Click(object sender, RoutedEventArgs e)
		{
			Value -= Step;
		}

		public new bool Focus()
		{
			return cValue.Focus();
		}

		private void cValue_TextChanged(object sender, TextChangedEventArgs e)
		{
			if (!cValue.Text.EndsWith(".") && !string.IsNullOrEmpty(cValue.Text))
			{
				double result = 0.0;
				if (!double.TryParse(cValue.Text, out result))
				{
					Value = Value;
				}
			}
		}

		private void cValue_LostFocus(object sender, RoutedEventArgs e)
		{
			double result = 0.0;
			Value = (double.TryParse(cValue.Text, out result) ? result : Value);
		}
	}
}
